import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import com.github.fge.jsonpatch.diff.JsonDiff;
import com.networknt.schema.ValidationMessage;

import java.io.IOException;
import java.util.Set;

/**
 * @author wjj
 */
public class JsonPatchTest {

    private static final String PAGE_ORIGINAL_FORM_DATA = "{\"map_policy\":[{\"marketplace_id\":\"\",\"value\":\"policy_10\"}],\"purchasable_offer\":[{\"map_price\":[{\"schedule\":[{\"value_with_tax\":null}]}],\"currency\":\"\",\"marketplace_id\":\"\",\"our_price\":[{\"schedule\":[{\"value_with_tax\":null}]}],\"minimum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":null}]}],\"maximum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":null}]}],\"discounted_price\":[{\"schedule\":[{\"end_at\":\"\",\"start_at\":\"\",\"value_with_tax\":null}]}],\"start_at\":{\"value\":\"\"},\"end_at\":{\"value\":\"\"},\"audience\":\"\",\"quantity_discount_plan\":[{\"schedule\":[{\"discount_type\":\"\",\"levels\":[{\"lower_bound\":null,\"value\":null}]}]}]}],\"condition_note\":[{\"language_tag\":\"\",\"marketplace_id\":\"\",\"value\":\"\"}],\"condition_type\":[{\"marketplace_id\":\"\",\"value\":\"\"}],\"skip_offer\":[{\"marketplace_id\":\"\",\"value\":false}],\"merchant_shipping_group\":[{\"marketplace_id\":\"\",\"value\":\"\"}],\"list_price\":[{\"currency\":\"\",\"marketplace_id\":\"\",\"value\":null}],\"main_offer_image_locator\":[{\"marketplace_id\":\"\",\"media_location\":\"\"}],\"gift_options\":[{\"can_be_messaged\":false,\"can_be_wrapped\":false,\"marketplace_id\":\"\"}],\"product_tax_code\":[{\"marketplace_id\":\"\",\"value\":\"\"}],\"other_offer_image_locator_5\":[{\"marketplace_id\":\"\",\"media_location\":\"\"}],\"other_offer_image_locator_4\":[{\"marketplace_id\":\"\",\"media_location\":\"\"}],\"other_offer_image_locator_3\":[{\"marketplace_id\":\"\",\"media_location\":\"\"}],\"other_offer_image_locator_2\":[{\"marketplace_id\":\"\",\"media_location\":\"\"}],\"other_offer_image_locator_1\":[{\"marketplace_id\":\"\",\"media_location\":\"\"}],\"fulfillment_availability\":[{\"fulfillment_channel_code\":\"\",\"quantity\":null,\"lead_time_to_ship_max_days\":null,\"restock_date\":\"\",\"is_inventory_available\":false}],\"merchant_release_date\":[{\"marketplace_id\":\"\",\"value\":\"\"}],\"max_order_quantity\":[{\"marketplace_id\":\"\",\"value\":null}]}";

    private static final String PAGE_UPDATED_FORM_DATA = "{\"map_policy\":[{\"marketplace_id\":\"\",\"value\":\"policy_10\"}],\"purchasable_offer\":[{\"map_price\":[{\"schedule\":[{\"value_with_tax\":2}]}],\"currency\":\"sdds\",\"marketplace_id\":\"\",\"our_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"minimum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"maximum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"discounted_price\":[{\"schedule\":[{\"end_at\":\"2025-02-10\",\"start_at\":\"2025-02-10\",\"value_with_tax\":7}]}],\"start_at\":{\"value\":\"2025-02-10\"},\"end_at\":{\"value\":\"2025-02-28\"},\"audience\":\"ALL\",\"quantity_discount_plan\":[{\"schedule\":[{\"discount_type\":\"fixed\",\"levels\":[{\"lower_bound\":2,\"value\":2}]}]}]}],\"condition_note\":[{\"language_tag\":\"\",\"marketplace_id\":\"\",\"value\":\"43fedfddfdffd\"}],\"condition_type\":[{\"marketplace_id\":\"\",\"value\":\"new_oem\"}],\"skip_offer\":[{\"marketplace_id\":\"\",\"value\":false}],\"merchant_shipping_group\":[{\"marketplace_id\":\"\",\"value\":\"432efrddfgdf\"}],\"list_price\":[{\"currency\":\"USD\",\"marketplace_id\":\"\",\"value\":0.06}],\"main_offer_image_locator\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"gift_options\":[{\"can_be_messaged\":false,\"can_be_wrapped\":false,\"marketplace_id\":\"\"}],\"product_tax_code\":[{\"marketplace_id\":\"\",\"value\":\"43refdgdfg\"}],\"other_offer_image_locator_5\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_4\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_3\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_2\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_1\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"fulfillment_availability\":[{\"fulfillment_channel_code\":\"AMAZON_NA\",\"quantity\":5,\"lead_time_to_ship_max_days\":2,\"restock_date\":\"2025-02-10\",\"is_inventory_available\":false}],\"merchant_release_date\":[{\"marketplace_id\":\"\",\"value\":\"2025-02-10\"}],\"max_order_quantity\":[{\"marketplace_id\":\"\",\"value\":10}]}";

    // 删除map_policy 测试remove
//    private static final String PAGE_UPDATED_FORM_DATA = "{\"purchasable_offer\":[{\"map_price\":[{\"schedule\":[{\"value_with_tax\":2}]}],\"currency\":\"sdds\",\"marketplace_id\":\"\",\"our_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"minimum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"maximum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"discounted_price\":[{\"schedule\":[{\"end_at\":\"2025-02-10\",\"start_at\":\"2025-02-10\",\"value_with_tax\":7}]}],\"start_at\":{\"value\":\"2025-02-10\"},\"end_at\":{\"value\":\"2025-02-28\"},\"audience\":\"ALL\",\"quantity_discount_plan\":[{\"schedule\":[{\"discount_type\":\"fixed\",\"levels\":[{\"lower_bound\":2,\"value\":2}]}]}]}],\"condition_note\":[{\"language_tag\":\"\",\"marketplace_id\":\"\",\"value\":\"43fedfddfdffd\"}],\"condition_type\":[{\"marketplace_id\":\"\",\"value\":\"new_oem\"}],\"skip_offer\":[{\"marketplace_id\":\"\",\"value\":false}],\"merchant_shipping_group\":[{\"marketplace_id\":\"\",\"value\":\"432efrddfgdf\"}],\"list_price\":[{\"currency\":\"USD\",\"marketplace_id\":\"\",\"value\":0.06}],\"main_offer_image_locator\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"gift_options\":[{\"can_be_messaged\":false,\"can_be_wrapped\":false,\"marketplace_id\":\"\"}],\"product_tax_code\":[{\"marketplace_id\":\"\",\"value\":\"43refdgdfg\"}],\"other_offer_image_locator_5\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_4\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_3\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_2\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_1\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"fulfillment_availability\":[{\"fulfillment_channel_code\":\"AMAZON_NA\",\"quantity\":5,\"lead_time_to_ship_max_days\":2,\"restock_date\":\"2025-02-10\",\"is_inventory_available\":false}],\"merchant_release_date\":[{\"marketplace_id\":\"\",\"value\":\"2025-02-10\"}],\"max_order_quantity\":[{\"marketplace_id\":\"\",\"value\":10}]}";

    private static final ObjectMapper om = new ObjectMapper();

    public static void main(String[] args) throws IOException, JsonPatchException {
        JsonNode source = om.readTree(PAGE_ORIGINAL_FORM_DATA);
        JsonNode target = om.readTree(PAGE_UPDATED_FORM_DATA);

        final JsonPatch patch = JsonDiff.asJsonPatch(source, target);
        // 差异
//        System.out.println(patch);
        // 合并
        JsonNode merge = patch.apply(source);
//        System.out.println(merge.toString());
        // 验证是否符合要求
        Set<ValidationMessage> messages = JsonSchemaValidator.validator(merge);
//        messages.forEach(System.out::println);

        // 组装成Amazon SP API Product Type Definition Api 修改时需要的格式
        System.out.println(new PatchedJsonGenerator().transform(merge));
    }

}
